package com.experienceBaby.Cashbook.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.experienceBaby.Cashbook.R;
import com.experienceBaby.Cashbook.adapter.ViewPagerAdapter;
import com.experienceBaby.Cashbook.greendao.DbController;
import com.flyco.tablayout.SlidingTabLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ChartFragments extends Fragment {
    View view;
    //isaBoolean:true月false年    aBoolean:false支出true收入
    boolean aBoolean, isaBoolean;
    ViewPager vp;
    SlidingTabLayout st;
    int year;
    int month;

    ChartFragments(boolean isaBoolean, boolean aBoolean) {
        this.isaBoolean = isaBoolean;
        this.aBoolean = aBoolean;
    }

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragments_chart, container, false);
        initView();

        return view;
    }

    private void initView() {
        vp = view.findViewById(R.id.vp);
        st = view.findViewById(R.id.st);
        Calendar now = Calendar.getInstance();

        year = now.get(Calendar.YEAR);
        month = now.get(Calendar.MONTH) + 1;

        if (isaBoolean) {
            initData_MONTH();
        } else {
            initData_YEAR();
        }

    }

    private void initData_MONTH() {
        ArrayList<Fragment> fragmentlist = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            fragmentlist.add(new ChartSmallFragment(year, i, aBoolean));
        }

        vp.setAdapter(new ViewPagerAdapter(getChildFragmentManager(), fragmentlist));
        st.setViewPager(vp, new String[]{"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月",
                "十月", "十一月", "十二月"});
//        st.setViewPager(vp, new String[]{"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月",
//                "十月", "十一月", "十二月"}, getActivity(), fragmentlist);

    }

    private void initData_YEAR() {
        DbController dbController = new DbController(getContext());
        List<Integer> integers = dbController.searchYear();
        String[] strings = new String[integers.size()];
        ArrayList<Fragment> fragmentlist = new ArrayList<>();
        if (integers.size() > 0) {
            for (int i = 0; i < integers.size(); i++) {
                fragmentlist.add(new ChartSmallFragment(year, 0, aBoolean));
                strings[i] = integers.get(i) + "年";
            }
            vp.setAdapter(new ViewPagerAdapter(getChildFragmentManager(), fragmentlist));
            st.setViewPager(vp, strings);
        }
//        st.setViewPager(vp, strings, getActivity(), fragmentlist);
    }
}
